<template>
  <a-modal
    title="新增"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="组件编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入组件编号" v-decorator="['code', {rules: [{required: true, message: '请输入组件编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="组件类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择组件类型"
            v-decorator="['type', {rules: [{required: true, message: '请选择组件类型！'}]}]" >
            <a-select-option value="1">图层</a-select-option>
            <a-select-option value="2">网格</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="层级"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择层级"
            v-decorator="['level', {rules: [{required: true, message: '请选择组件类型！'}]}]" >
            <a-select-option value="1">省</a-select-option>
            <a-select-option value="2">市</a-select-option>
            <a-select-option value="3">区</a-select-option>
            <a-select-option value="4">网格</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="封面链接"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入封面链接" v-decorator="['picUrl']" />
          <a-upload
            :customRequest="customRequest"
            :multiple="false"
            :showUploadList="false"
            name="file"
          >
            <a-button> <a-icon type="upload" />上传文件</a-button>
          </a-upload>
        </a-form-item>
        <a-form-item
          label="数据来源"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数据来源" v-decorator="['dataSource']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { biComponentAdd } from '@/api/modular/main/bicomponent/biComponentManage'
  import { sysFileInfoUpload } from '@/api/modular/system/fileManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        typeData: [],
        levelData: [],
        picUrl: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        const typeOption = this.$options
        this.typeData = typeOption.filters['dictData']('yes_or_no')
        const levelOption = this.$options
        this.levelData = levelOption.filters['dictData']('vis_type')
      },
      /**
       * 上传文件
       */
      customRequest (data) {
        const formData = new FormData()
        formData.append('file', data.file)
        sysFileInfoUpload(formData).then((res) => {
          if (res.success) {
            this.$message.success('上传成功')
            this.picUrl = process.env.VUE_APP_API_BASE_URL + '/sysFileInfo/preview?id=' + res.data
            this.form.setFieldsValue(
              {
                picUrl: this.picUrl
              })
          } else {
            this.$message.error('上传失败：' + res.message)
          }
        })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            biComponentAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
